package cn.maihe.elg.operation.centers.fuzhou.channel;

import cn.maihe.elg.operation.centers.fuzhou.channel.service.*;
import cn.maihe.elg.operation.centers.fuzhou.channel.service.FZBondDownloadChannelService;
import cn.maihe.elg.operation.centers.fuzhou.channel.service.FZClaimResultReceiveService;
import cn.maihe.elg.operation.centers.fuzhou.dto.FZBaseRespDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.channel.FZNoticeReqDTO;
import cn.maihe.elg.operation.centers.fuzhou.enums.HyMakeBackStatusEnum;
import cn.maihe.elg.operation.centers.fuzhou.enums.HyMakeRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.CenterInterfaceService;
import cn.maihe.elg.operation.centers.supports.model.bo.*;
import cn.maihe.elg.operation.config.ZtAccountInfo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.RefundOrderStatus;
import cn.maihe.elg.operation.repository.entity.RefundOrderInfoDO;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.NoticeRetryInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.service.system.impl.OrganizationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description 武夷山海迈公共资源服务中心接口服务实现
 * @Author WJH
 * @Date 2021/05/26
 */
@Service
@Slf4j
public abstract class BaseFZCenterInterfaceService implements CenterInterfaceService {

    @Resource
    FZGuaranteeBackNoticeService fzMakeGuaranteeBackNoticeService;
    @Resource
    FZClaimResultReceiveService fzMakeClaimResultReceiveService;

    @Resource
    private FZPayResultReceiveChannelService payResultReceiveChannelService;
    @Resource
    private FZBondDownloadChannelService bondDownloadChannelService;

    @Resource
    private RefundOrderInfoService refundOrderInfoService;
    @Resource
    private ProjectInfoService projectInfoService;
    @Resource
    private InstitutionService institutionService;
    @Resource
    private ZtAccountInfo ztAccountInfo;
    @Resource
    private OrganizationServiceImpl organizationService;
    @Resource
    private NoticeRetryInfoService noticeRetryInfoService;
    @Resource
    private GuaranteeService guaranteeService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;


    @Override
    public InnerBaseResp<NoticeGuaranteeApplyResultBo> noticePayResult(NoticePayResultBo bo) {
       return payResultReceiveChannelService.noticePayResult(bo);
    }

    @Override
    public InnerBaseResp<String> pushGuarantee(PushCenterGuaranteeBo bo) {
        return bondDownloadChannelService.bondDownload(bo);
    }

    /**
     * 保函撤销|退保结果通知
     *
     * @param bo
     * @return
     */
    @Override
    public InnerBaseResp noticeGuaranteeRevokeResult(GuaranteeRevokeResultBo bo) {
        FZNoticeReqDTO reqDTO = new FZNoticeReqDTO();
        reqDTO.setPolicyNo(bo.getGuaranteeNo());
        InnerBaseResp<String> innerBaseResp = null;

        if(bo.getAuditPass()){
            reqDTO.setBackStatus(HyMakeBackStatusEnum.SUCCESS.getStatus());
            reqDTO.setBackMsg(bo.getAuditRemark());
        } else{
            reqDTO.setBackStatus(HyMakeBackStatusEnum.FAILURE.getStatus());
            reqDTO.setBackMsg(bo.getAuditRemark());
        }

        try {
            FZBaseRespDTO respDTO = fzMakeGuaranteeBackNoticeService.doRequest(getCenterNoEnum(), reqDTO);
            if (HyMakeRespCodeEnum.isSuccess(respDTO.getCode())) {
                innerBaseResp = InnerBaseResp.success(reqDTO.getPolicyNo());
            } else {
                innerBaseResp = InnerBaseResp.error(respDTO.getMessage());
            }

        } catch (Exception e) {
            log.error("保函撤销结果通知 >> [{}] 异常:{}", getCenterNoEnum().getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }

    /**
     * 保函理赔申请结果通知
     *
     * @param bo
     * @return
     */
    @Override
    public InnerBaseResp noticeGuaranteeClaimSettlementResult(GuaranteeClaimSettlementResultBo bo) {
        FZNoticeReqDTO reqDTO = new FZNoticeReqDTO();
        RefundOrderInfoDO refundOrderInfo = bo.getRefundOrderInfo();
        reqDTO.setPolicyNo(refundOrderInfo.getGuaranteeNo());

        InnerBaseResp<String> innerBaseResp = null;
        if (refundOrderInfo.getStatus() == RefundOrderStatus.REFUNDED.getStatus()) {
            reqDTO.setBackStatus(HyMakeBackStatusEnum.SUCCESS.getStatus());
            reqDTO.setBackMsg(refundOrderInfo.getRemark());
        } else if (refundOrderInfo.getStatus() == RefundOrderStatus.REFUSED.getStatus()) {
            reqDTO.setBackStatus(HyMakeBackStatusEnum.FAILURE.getStatus());
            reqDTO.setBackMsg(refundOrderInfo.getRemark());
        } else {
            return
                    innerBaseResp = InnerBaseResp.success(refundOrderInfo.getGuaranteeNo());
        }

        try {
            FZBaseRespDTO respDTO = fzMakeClaimResultReceiveService.doRequest(getCenterNoEnum(), reqDTO);
            if (HyMakeRespCodeEnum.isSuccess(respDTO.getCode())) {
                innerBaseResp = InnerBaseResp.success(reqDTO.getPolicyNo());
            } else {
                innerBaseResp = InnerBaseResp.error(respDTO.getMessage());
            }

        } catch (Exception e) {
            log.error("理赔结果通知 >> [{}] 异常:{}", getCenterNoEnum().getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }

}
